﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using Gift.DAL;
using Gift.Model;

namespace WebSite.cn
{
    public partial class products_details : BasePage
    {
         // Fields
                                                                                                          
    public string classnames = "";
    public string clicks = "0";

    public string h3 = "0";
    public string h4 = "0";


    public string images = "";
    public string imgs = "";

    public string pic = "";
    public string price = "0";
    public string procode = "";
    public string procolor = "";
    public string procolora = "";
    public string procolorb = "";
    public string proimg = "";
    public string proname = "";
    public string propinpai = "";
    public string proprice1 = "0";
    public string proprice3 = "0";
    public string proscore = "0";
    public string prosize = "";
    public string prosizea = "";
    public string prosizeb = "";
    public string typename = "";

    public string SPriceName = "";
    public string Instruction = "";

    readonly Shoppro theShoppro = new Shoppro();

    // Methods
    private void Bind()
    {
        DataTable table = SQLAccess.GreatDs("select proid,typeid,proname,iscuxao,procode,propinpai,procontent,Instruction,proprice4,proprice1,prokey,proprice3,propic1,proscore1,propic2,clicks,proisnew3 from DB_Shoppro where proid=" + base.Request.QueryString["pid"] + "").Tables[0];
        if (table.Rows.Count > 0)
        {
            this.proname = table.Rows[0]["proname"].ToString();
            this.procode = table.Rows[0]["procode"].ToString();
            this.proprice3 = string.Format("{0:f}", table.Rows[0]["proprice3"]);
            this.proprice1 = string.Format("{0:f}", table.Rows[0]["proprice1"]);
            this.price = table.Rows[0]["proprice1"].ToString();
            this.SPriceName = table.Rows[0]["proisnew3"].ToString().ToString().ToLower() == "true" ? "零售价：" : "兑换价：";
            this.proscore = table.Rows[0]["proscore1"].ToString();
            this.Instruction = table.Rows[0]["Instruction"].ToString();
            string str3 = Displaya(table.Rows[0]["typeid"].ToString(), "1");
            if (str3.ToString() != "")
            {
                string[] strArray = str3.ToString().Split(new char[] { '|' });
                for (int i = strArray.Length - 1; i >= 0; i--)
                {
                    DataTable table2 = SQLAccess.GreatDs("select classname,classid from DB_Productclass where classid=" + strArray[i] + "").Tables[0];
                    if (table.Rows.Count > 0)
                    {
                        if (table.Rows[0]["typeid"].ToString() == strArray[i].ToString())
                        {
                            this.classnames = this.classnames + " > <span class='text_red03'>" + table2.Rows[0]["classname"].ToString() + "</span>";
                        }
                        else
                        {
                            string classnames = this.classnames;
                            this.classnames = classnames + " > <a href='products_category.aspx?typeid=" + table2.Rows[0]["classid"].ToString() + "'>" + table2.Rows[0]["classname"].ToString() + "</a>";
                        }
                    }
                }
            }
            if (Convert.ToBoolean(table.Rows[0]["iscuxao"]))
            {
                this.proprice1 = string.Format("{0:f}", table.Rows[0]["proprice4"]);
                this.price = table.Rows[0]["proprice4"].ToString();
            }
            if (table.Rows[0]["prokey"].ToString() != "")
            {
                string[] strArray2 = table.Rows[0]["prokey"].ToString().Split(new char[] { ',' });
                for (int j = 0; j < strArray2.Length; j++)
                {
                    object prosize = this.prosize;
                    this.prosize = string.Concat(new object[] { prosize, "<li class=' null' id='ark:2559:", j, "' title='", strArray2[j], "'><a href='#' onclick='return sellProperty.observe(this);' cpid='2559' pvid='2559:", j, "'><span>", strArray2[j], "</span></a><i id='ark:2559:", j, ":i' style='display:none'>selected</i></li>" });
                    object prosizea = this.prosizea;
                    this.prosizea = string.Concat(new object[] { prosizea, " '2559:", j, "' : {" });
                    this.prosizea = this.prosizea + "'name' : '" + strArray2[j].ToString() + "',";
                    this.prosizea = this.prosizea + "'cpid' : '2559',";
                    object obj4 = this.prosizea;
                    this.prosizea = string.Concat(new object[] { obj4, "'pvid' : '2559:", j, "'," });
                    this.prosizea = this.prosizea + "'selected' : false,";
                    this.prosizea = this.prosizea + "'instock' :  true ,";
                    this.prosizea = this.prosizea + "'promotionItemMeal':false";
                    if (j == (strArray2.Length - 1))
                    {
                        this.prosizea = this.prosizea + "}";
                    }
                    else
                    {
                        this.prosizea = this.prosizea + "},";
                    }
                    object prosizeb = this.prosizeb;
                    this.prosizeb = string.Concat(new object[] { prosizeb, "2559:", j, ";" });
                }
            }
            else
            {
                this.prosize = this.prosize + "<li class=' null' id='ark:2559:0' title='均码'><a href='#' onclick='return sellProperty.observe(this);' cpid='2559' pvid='2559:0'><span>均码</span></a><i id='ark:2559:0:i' style='display:none'>selected</i></li> ";
                this.prosizea = this.prosizea + " '2559:0' : {";
                this.prosizea = this.prosizea + "'name' : '均码',";
                this.prosizea = this.prosizea + "'cpid' : '2559',";
                this.prosizea = this.prosizea + "'pvid' : '2559:0',";
                this.prosizea = this.prosizea + "'selected' : false,";
                this.prosizea = this.prosizea + "'instock' :  true ,";
                this.prosizea = this.prosizea + "'promotionItemMeal':false";
                this.prosizea = this.prosizea + "}";
                this.prosizeb = this.prosizeb + "2559:0;";
            }
            this.images = "../../uploadfile/product/" + table.Rows[0]["propic1"].ToString();
            this.proimg = "../../uploadfile/product/" + table.Rows[0]["propic1"].ToString();
            this.clicks = table.Rows[0]["clicks"].ToString();
            this.imgs = "<SCRIPT>";
            string pic = this.pic;
            this.pic = pic + "<TD id=original_image onmouseover=displayImage('original_image','../../uploadfile/product/" + table.Rows[0]["propic1"].ToString() + "') class=border_2  width=20%><TABLE cellSpacing=0 cellPadding=0 border=0><TR><TD><A  href='pic.aspx?pid=" + table.Rows[0]["proid"].ToString() + "' target=_blank><IMG   src='../../uploadfile/product/" + table.Rows[0]["propic2"].ToString() + "' width=60px; border=0></A></TD></TR></TABLE></TD>";
            DataTable table3 = SQLAccess.GreatDs("select * from DB_Shoppic where proid=" + re.replace(base.Request.QueryString["pid"]) + " order by picid asc").Tables[0];
            if (table3.Rows.Count > 0)
            {
                int num3 = 1;
                foreach (DataRow row in table3.Rows)
                {
                    object obj6 = this.pic;
                    this.pic = string.Concat(new object[] { obj6, "<TD id=cust_image_", num3, "  onmouseover=displayImage('cust_image_", num3, "','../../uploadfile/", row["picurl"].ToString().Replace(@"\", "/"), "') class=border_1 width=25%><TABLE cellSpacing=0 cellPadding=0 border=0><TR><TD><A  href=pic1.aspx?pid=", row["proid"], "&picid=", row["picid"], " target=_blank><IMG  src=../../uploadfile/", row["picurl"], " width=60 border=0></A></TD></TR></TABLE></TD> " });
                    object imgs = this.imgs;
                    this.imgs = string.Concat(new object[] { 
                        imgs, "registerImage('cust_image_", num3, "', '../../uploadfile/", row["picurl"].ToString().Replace(@"\", "/"), "', '<a href=pic1.aspx?pid=", row["proid"], "&picid=", row["picid"], " target=_blank><img src=../../uploadfile/", row["picurl"].ToString().Replace(@"\", "/"), "  width=", Convert.ToSingle(this.h3), " height=", Convert.ToSingle(this.h4), " border=0 /></a>', '", 
                        row["content"], "');"
                     });
                    num3++;
                    if ((num3 % 4) == 0)
                    {
                        this.pic = this.pic + "</tr><tr>";
                    }
                }
            }
            table3 = SQLAccess.GreatDs("select * from DB_Spe where proid=" + re.replace(base.Request.QueryString["pid"]) + " order by speid asc").Tables[0];
            if (table3.Rows.Count > 0)
            {
                int num4 = 1;
                foreach (DataRow row2 in table3.Rows)
                {
                    if (row2["spepic"].ToString() != "")
                    {
                        object procolor = this.procolor;
                        this.procolor = string.Concat(new object[] { procolor, "<li class='outofstock' id='ark:2558:", row2["speid"], "' title='", row2["spename"].ToString(), "'><a href='#' style=' background-image:url(../uploadfile/", row2["spepic"].ToString().Replace(@"\", "/"), ")' onclick='return sellProperty.observe(this);' cpid='2558' pvid='2558:", row2["speid"], "'><span>", row2["spename"], "</span></a><i id='ark:2558:", row2["speid"], ":i' style='display:none'>selected</i></li> " });
                    }
                    else
                    {
                        object obj9 = this.procolor;
                        this.procolor = string.Concat(new object[] { obj9, "<li class=' null' id='ark:2558:", row2["speid"], "' title='", row2["spename"].ToString(), "'><a href='#' onclick='return sellProperty.observe(this);' cpid='2558' pvid='2558:", row2["speid"], "'><span>", row2["spename"].ToString(), "</span></a><i id='ark:2558:", row2["speid"], ":i' style='display:none'>selected</i></li>" });
                    }
                    this.procolora = this.procolora + " '2558:" + row2["speid"].ToString() + "' : {";
                    this.procolora = this.procolora + "'name' : '" + row2["spename"].ToString() + "',";
                    this.procolora = this.procolora + "'cpid' : '2558',";
                    this.procolora = this.procolora + "'pvid' : '2558:" + row2["speid"].ToString() + "',";
                    this.procolora = this.procolora + "'selected' : false,";
                    this.procolora = this.procolora + "'instock' :  true ,";
                    this.procolora = this.procolora + "'promotionItemMeal':false";
                    if (num4 == table3.Rows.Count)
                    {
                        this.procolora = this.procolora + "}";
                    }
                    else
                    {
                        this.procolora = this.procolora + "},";
                    }
                    this.procolorb = this.procolorb + "{";
                    object procolorb = this.procolorb;
                    this.procolorb = string.Concat(new object[] { procolorb, "'detailId' : '", num4, "'," });
                    this.procolorb = this.procolorb + "'prop' \t: ';";
                    this.procolorb = this.procolorb + this.prosizeb;
                    this.procolorb = this.procolorb + "2558:" + row2["speid"].ToString() + ";',";
                    this.procolorb = this.procolorb + "'price' : '59.00',";
                    this.procolorb = this.procolorb + "'discount' : '3.7',";
                    this.procolorb = this.procolorb + "'point' : '29',";
                    this.procolorb = this.procolorb + "'save'\t: '99.00',";
                    this.procolorb = this.procolorb + "'stock' : '10'";
                    if (num4 == table3.Rows.Count)
                    {
                        this.procolorb = this.procolorb + "}";
                    }
                    else
                    {
                        this.procolorb = this.procolorb + "},";
                    }
                    num4++;
                }
            }
            if (this.procolor == "")
            {
                this.procolor = string.Concat(new object[] { "<li class=' null' id='ark:2558:", table.Rows[0]["proid"], "' title='均色'><a href='#' style=' ' onclick='return sellProperty.observe(this);' cpid='2558' pvid='2558:", table.Rows[0]["proid"], "'><span>均色</span></a><i id='ark:2558:", table.Rows[0]["proid"], ":i' style='display:none'>selected</i></li>" });
                object procolora = this.procolora;
                this.procolora = string.Concat(new object[] { procolora, " '2558:", table.Rows[0]["proid"], "' : {" });
                this.procolora = this.procolora + "'name' : '均色',";
                this.procolora = this.procolora + "'cpid' : '2558',";
                object obj12 = this.procolora;
                this.procolora = string.Concat(new object[] { obj12, "'pvid' : '2558:", table.Rows[0]["proid"], "'," });
                this.procolora = this.procolora + "'selected' : false,";
                this.procolora = this.procolora + "'instock' :  true ,";
                this.procolora = this.procolora + "'promotionItemMeal':false";
                this.procolora = this.procolora + "}";
                this.procolorb = this.procolorb + "{";
                this.procolorb = this.procolorb + "'detailId' : '0',";
                this.procolorb = this.procolorb + "'prop' \t: ';";
                this.procolorb = this.procolorb + this.prosizeb;
                object obj13 = this.procolorb;
                this.procolorb = string.Concat(new object[] { obj13, "2558:", table.Rows[0]["proid"], ";'," });
                this.procolorb = this.procolorb + "'price' : '59.00',";
                this.procolorb = this.procolorb + "'discount' : '3.7',";
                this.procolorb = this.procolorb + "'point' : '29',";
                this.procolorb = this.procolorb + "'save'\t: '99.00',";
                this.procolorb = this.procolorb + "'stock' : '10'";
                this.procolorb = this.procolorb + "}";
            }
            this.imgs = this.imgs + "</SCRIPT>";
        }
    }

    public static string Displaya(string classid, string languages)
    {
        string str2 = "";
        string sql = "select top 1 parentid,classid from DB_Productclass where classid=" + classid + " and languages=" + languages + " and visible= 1 order by classorder";
        DataTable table = new DataTable();
        table = SQLAccess.GreatDs(sql).Tables[0];
        if (table.Rows.Count > 0)
        {
            str2 = str2 + table.Rows[0]["classid"].ToString() + "";
            if (table.Rows[0]["parentid"].ToString() != "0")
            {
                str2 = str2 + "|" + Displaya(table.Rows[0]["parentid"].ToString(), languages);
            }
        }
        return str2.ToString();
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string str2;
        DataTable table;

        DB_Shoppro oShoppro;
        oShoppro = theShoppro.GetModel(int.Parse(re.replace(base.Request.Form["pid"].ToString())));


        tbCardType oCardType = new tbCardType();
        DALCardType theCardType = new DALCardType();
      
        string sPid = base.Request.Form["pid"];
        string sPrice = base.Request.Form["price"];
        string sContent =  "";
        string sProNum = this.pronum.Value;

        if (oShoppro.proisnew2 == true )
        {
            if (UserCardCodeList == null)
            {
                Response.Write("<script>window.location.href='ProfessionArea.aspx?sid=2';</script>");
              //  Response.Write("<script>window.location.href='ProfessionArea.aspx?msg=1&pid=" + sPid + "&price=" + sPrice + "&content=" + sContent + "&pronum=" + sProNum + "&type=1';</script>");
                return;
            }
            oCardType = theCardType.GetModel(int.Parse(UserCardCodeList[0].UserCardTypeId));
            if (oShoppro.ProfessionAreaTypeId != oCardType.ProfessionAreaTypeId)
            {
                Response.Write("<script>window.location.href='ProfessionArea.aspx?sid=2';</script>");
                return;
            }
        }
        else if (oShoppro.proisnew4 == true)
        {
            if (UserMemberId == null)
            {
                Response.Write("<script>window.location.href='login.aspx?msg=1&pid=" + sPid + "&price=" + sPrice + "&content=" + sContent + "&pronum=" + sProNum + "&type=2';</script>");
                return;
            }
        }
        else if (UserCardCodeList != null)
        {
            if (oShoppro.proisnew2 != true)
            {
                Response.Write("<script>alert('您已用卡登录，请到【我要兑换】区选相关商品！');window.location.href='ProfessionArea.aspx?sid=2';</script>");
                return;
            }
            oCardType = theCardType.GetModel(int.Parse(UserCardCodeList[0].UserCardId));
            if (oShoppro.ProfessionAreaTypeId != oCardType.ProfessionAreaTypeId)
            {
                Response.Write("<script>window.location.href='ProfessionArea.aspx?msg=1&pid=" + sPid + "&price=" + sPrice + "&content=" + sContent + "&pronum=" + sProNum + "&type=1';</script>");
                return;
            }

        }
        else if (UserMemberId != null)
        {

            if (oShoppro.proisnew3 == true)
            {
               // UserMemberId = null;
            }else  if (oShoppro.proisnew4 != true)
            {
                Response.Write("<script>window.location.href='hotpro.aspx?sid=4&pid=" + sPid + "&price=" + sPrice + "&content=" + sContent + "&pronum=" + sProNum + "&type=2';</script>");
                return;
            }
        }

        string str3 = "0";
        string str4 = "";
        string str5 = "0";
        str3 = base.Request.Form["price"].ToString();
        str4 = base.Request.Form["content"].ToString();
        str5 = this.pronum.Value;

        if (base.TempOrderId == null)
        {

            if (re.ReadCookie("tempno") != null)
            {
                base.TempOrderId =re.ReadCookie("tempno");
            }
            else
            {
                base.TempOrderId = DateTime.Now.Ticks.ToString();
                re.WriteCookie("tempno", base.TempOrderId, 30);
            }

        }


       string sType = "0";
        if (UserCardCodeList != null)
        {
            SQLAccess.GreatDs("delete from DB_Car where carid in( select DB_Car.carid from DB_Car inner join DB_Shoppro on DB_Car.productid=DB_Shoppro.proid where DB_Car.flag=0 and DB_Car.temp='" + base.TempOrderId + "')");
            sType = "1";
        }
        else if (UserMemberId != null)
        {
       
            sType = "2";
        }
        else
        {   
            sType = "0";
        }

        table = SQLAccess.GreatDs("select top 1 carid,pronum,price,score from DB_Car where productid=" + re.replace(base.Request.Form["pid"].ToString()) + " and temp=" + base.TempOrderId + " and contents='" + str4 + "' and flag=0").Tables[0];
        if (table.Rows.Count > 0)
        {
            str2 = "update DB_Car set pronum=" + str5 + ",sumprice=" + Convert.ToString((float) (Convert.ToSingle(str5) * Convert.ToSingle(table.Rows[0]["price"]))) + ",sumscore=" + Convert.ToString((float) (Convert.ToSingle(str5) * Convert.ToSingle(table.Rows[0]["score"]))) + " where carid=" + table.Rows[0]["carid"].ToString() + "";
        }
        else
        {
            str2 = "insert into DB_Car (productid,memberid,pronum,state,flag,price,contents,temp,sumprice,score,sumscore) values (" + re.replace(base.Request.Form["pid"].ToString()) + ",0,"+ str5 + ",0,0," + str3.ToString() + ",'" + str4 + "'," + base.TempOrderId + "," + Convert.ToString((float)(Convert.ToSingle(str5) * Convert.ToSingle(str3))) + "," + base.Request.Form["score"] + "," + Convert.ToString((float)(Convert.ToSingle(str5) * Convert.ToSingle(base.Request.Form["score"]))) + ")";
        }
        SQLAccess.DoSql(str2);
        base.Response.Write("<script language='javascript'>window.location.href='account.aspx?type=" + sType +"';</script>");
        base.Response.End();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.IsPostBack)
        {
            this.SaveLastBrowesProduct();
            string sql = "Select top 1 shopwidth1,shopheight1 From DB_Shopconfig";
            DataTable table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                this.h3 = table.Rows[0]["shopwidth1"].ToString();
                this.h4 = table.Rows[0]["shopheight1"].ToString();
            }
            if (base.Request.QueryString["sid"] != null)
            {
                table = SQLAccess.GreatDs("select shuxinid,shuxiname from DB_Shuxintype where shuxinid=" + base.Request.QueryString["sid"] + "").Tables[0];
                if (table.Rows.Count > 0)
                {
                    string classnames = this.classnames;
                    this.classnames = classnames + " > <a href='hotpro.aspx?sid=" + table.Rows[0][0].ToString() + "'>" + table.Rows[0][1].ToString() + "</a>";
                }
            }
            if (base.Request.QueryString["pid"] != null)
            {
                this.Bind();
                SQLAccess.DoSql("update DB_Shoppro set clicks=clicks+1 where proid=" + base.Request.QueryString["pid"] + "");
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["sid"]) && base.Request.QueryString["sid"] == "2")
            {
                this.ImageButton1.ImageUrl = "images/exchange.gif";
            }
            else
            {
                this.ImageButton1.ImageUrl = "images/dinggou.gif";
            }
            if (base.UserCardCodeList != null)
            {
                this.pronum.Value = base.UserCardCodeList.Count.ToString();
                this.pronum.Disabled = true;
            }
        }
    }

    string Proid
    {
        get {
            return base.Request.QueryString["pid"];
        }
     }


    void SaveLastBrowesProduct()
    {
        string strSQL = string.Empty;
        strSQL = "If NOT EXISTS(select 1 from tblLastBrowesProduct where proid = {0} and ip = '{1}') ";
        strSQL += "INSERT INTO tblLastBrowesProduct(proid,ip) values({2},'{3}')";
        SQLAccess.DoSql(string.Format(strSQL, Proid, ClientIP, Proid, ClientIP));
     }


    }
}